<template>
  <div class="user-info-form">
    <el-form
      :model="importMinStationForm"
      ref="importMinStationForm"
      @submit.native.prevent
    >
      <el-form-item label="" prop="organizationId">
        <el-select filterable v-model="organizationId">
          <el-option
            v-for="item in orgList"
            :key="item.organizationId"
            :label="item.shortName"
            :value="item.organizationId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="pictures" v-if="isShow" style="text-align: center;">
        <el-upload
          class="upload-asset"
          ref="upload"
          drag
          name="excelfile"
          accept=".xls,.xlsx"
          :action="actionUrl"
          :before-upload="beforeUpload"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-success="handleSuccess"
          :on-error="handleError"
          :file-list="fileList"
          :auto-upload="false"
          multiple
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">*只能上传xls/xlsx文件</div>
        </el-upload>
        <el-row class="download-temp-wrap" style="background-color: #E6E6E6;">
          <span>请使用模板来上传你要添加的微型消防站</span
          ><a :href="downloadTempUrl">下载模板</a>
        </el-row>
      </el-form-item>
      <el-row class="verifyidentity-button">
        <el-button @click.stop="closeFun" round>取消</el-button>
        <el-button type="primary" @click="confirm()" round>确定</el-button>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { getOrgList } from "@/api/org";
import { setTimeout } from "timers";

export default {
  name: "importMinFile",
  props: {
    closeFun: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      fileList: [],
      orgList: [],
      organizationId: "",
      actionUrl: "",
      /*actionUrl: this.GLOBAL.baseURL + 'miniFireHouse/addMiniFireHouses?orgId=0',*/
      importMinStationForm: {},
      isShow: false,
      downloadTempUrl: this.GLOBAL.baseURL + "miniFireHouse/downATemplate"
    };
  },
  created() {
    var that = this;

    // 获取业主单位信息
    getOrgList({
      queryType: 0
    }).then(res => {
      if ((res.status = 200)) {
        that.organizationId = res.data[0].organizationId;
        that.orgList = res.data;
      }
    });
  },
  methods: {
    beforeUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      const isXSL = testmsg === "xls";
      const isXLSX = testmsg === "xlsx";
      if (!isXSL && !isXLSX) {
        this.$message({
          message: "上传文件只能是 xls、xlsx格式!",
          type: "warning"
        });
        return false;
      }
    },
    handleRemove(file, fileList) {
      // console.log(file, fileList)
    },
    handlePreview(file) {
      // console.log(file);
    },
    handleSuccess(res) {
      if (res.status === 200) {
        this.$message.success("文件上传成功！");
      } else {
        this.$message.warning(res.info);
      }
      this.closeFun(true);
    },
    handleError(err, file, fileList) {
      this.$message.error("文件上传失败！");
    },
    confirm() {
      var that = this;
      that.$refs.upload.submit();
      that.closeFun(true);
    }
  },
  watch: {
    organizationId(val, oldVal) {
      if ((val || val == 0) && val !== oldVal) {
        this.isShow = false;
        this.actionUrl =
          this.GLOBAL.baseURL + "miniFireHouse/addMiniFireHouses?orgId=" + val;
        this.isShow = true;
      }
    }
  }
};
</script>
